package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.TargetAccount;

@Component
public class TargetDAO {

	 @PersistenceContext
	  private EntityManager em;
	/**
	 * all target account from database
	 * @return
	 */
	 @Transactional
	public List<TargetAccount> getTargetAccounts() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<TargetAccount> query = em.createQuery("SELECT a FROM TargetAccount a", TargetAccount.class);
		List<TargetAccount> targetAccounts = null;
		targetAccounts = query.getResultList();
		return targetAccounts;
	}
	/**
	 * get target account by target account number
	 * @param accountNum
	 * @return
	 */
	 @Transactional
	public TargetAccount getTargetAccount (String accountNum) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<TargetAccount> query = em.createQuery("SELECT c FROM TargetAccount c WHERE c.accountNumber = :id", TargetAccount.class);
		TargetAccount targetAccount = query.setParameter("id", accountNum).getSingleResult();
		return targetAccount;
	}
}
